package com.cnswhy.cloud.server.core.appservice.impl;

import com.alibaba.fastjson.JSON;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.common.core.utils.CnswhyUtil;
import com.cnswhy.cloud.common.redis.annotation.RedisLock;
import com.cnswhy.cloud.common.redis.model.LockTimeoutStrategy;
import com.cnswhy.cloud.common.redis.service.IdGeneratorService;
import com.cnswhy.cloud.server.common.entity.enumeration.TransType;
import com.cnswhy.cloud.server.common.entity.enumeration.WalletType;
import com.cnswhy.cloud.server.core.appservice.AppChargeMoneyService;
import com.cnswhy.cloud.server.core.entity.ChargeMoney;
import com.cnswhy.cloud.server.core.entity.CouponDetail;
import com.cnswhy.cloud.server.core.entity.Customer;
import com.cnswhy.cloud.server.core.entity.CustomerWallet;
import com.cnswhy.cloud.server.core.service.ChargeMoneyService;
import com.cnswhy.cloud.server.core.service.customer.CouponDetailService;
import com.cnswhy.cloud.server.core.service.customer.CustomerService;
import com.cnswhy.cloud.server.core.wallet.WalletService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class AppChargeMoneyServiceImpl implements AppChargeMoneyService {
    @Autowired
    ChargeMoneyService chargeMoneyService;
    @Autowired
    CustomerService customerService;
    @Autowired
    CouponDetailService couponDetailService;
    @Autowired
    WalletService walletService;
    @Autowired
    IdGeneratorService idGeneratorService;
    @Override
    @RedisLock(name = "charge", waitTime = 10, leaseTime = 60, keys = {"#tradeNo"}, lockTimeoutStrategy = LockTimeoutStrategy.FAIL_FAST)
    public ChargeMoney charge(String outTradeNo, String tradeNo) {
        /*log.info("chargeMoneyService.getOne:{}",outTradeNo);
        ChargeMoney order = chargeMoneyService.getOne(Wraps.<ChargeMoney>lbQ().eq(ChargeMoney::getOrderNum, outTradeNo));
        if (!(order.getStatus().equals("1"))) {
            return null;
        }
        log.info("customerService.getById:{}",order.getCustomerId());
        Customer customer = customerService.getById(order.getCustomerId());
        CustomerWallet customerWallet = walletService.checkAndCreate(customer.getId(), WalletType.COUPON, CnswhyUtil.getFen(customer.getCoupon()));
        walletService.add(customerWallet.getCustomerId(),WalletType.COUPON, TransType.COUPON_CHARGE,order.getChargeMoney(),TransType.COUPON_CHARGE.getCode() + order.getOrderNum());
        customerWallet = walletService.checkAndCreate(customer.getId(), WalletType.TOTAL_COUPON, CnswhyUtil.getFen(customer.getTotalCoupon()));
        walletService.add(customerWallet.getCustomerId(),WalletType.TOTAL_COUPON, TransType.COUPON_CHARGE,order.getChargeMoney(),TransType.COUPON_CHARGE.getCode() + order.getOrderNum());*/
        /*BigDecimal coupon = customer.getCoupon() == null ? BigDecimal.ZERO : customer.getCoupon();
        BigDecimal total = customer.getTotalCoupon() == null ? BigDecimal.ZERO : customer.getTotalCoupon();
        customer.setTotalCoupon(total.add(order.getChargeMoney()));
        customer.setCoupon(coupon.add(order.getChargeMoney()));
        log.info("customer.updateById:{}",customer.getId());
        customerService.updateById(customer);*/
        /*CouponDetail detail = new CouponDetail();
        detail.setCustomerId(customer.getId());
        detail.setCouponNum(order.getChargeMoney());
        detail.setType(1);
        detail.setOrderNum(outTradeNo);
        log.info("couponDetailService.save:{}", JSON.toJSONString(detail));
        couponDetailService.save(detail);*/
        return null;
    }
}
